export module user_interface;

import <iostream>;
import <format>;
import <string>;

import database;

namespace Records
{
	export
	{
		int displayMenu()
		{
			int selection{};
			std::cout << std::endl;
			std::cout << "Employee Database" << std::endl;
			std::cout << "-----------------" << std::endl;
			std::cout << " 1) Hire an employee" << std::endl;
			std::cout << " 2) Fire an employee" << std::endl;
			std::cout << " 3) Promote an employee" << std::endl;
			std::cout << " 4) Display all employees" << std::endl;
			std::cout << " 5) Display current employees" << std::endl;
			std::cout << " 6) Display former employees" << std::endl;
			std::cout << " 0) Exit" << std::endl;
			std::cout << std::endl;
			std::cout << "---> ";
			std::cin >> selection;

			return selection;
		}

		void doHire(Database &db)
		{
			std::string firstName{};
			std::string lastName{};

			std::cout << "First name? ";
			std::cin >> firstName;

			std::cout << "Last name? ";
			std::cin >> lastName;

			auto &employee{ db.addEmployee(firstName, lastName) };
			std::cout << std::format("Hired employee {} {} with employee number {}.",
				firstName, lastName, employee.getEmployeeNumber()) << std::endl;
		}

		void doFire(Database &db)
		{
			int employeeNumber{};
			std::cout << "Employee number? ";
			std::cin >> employeeNumber;

			try
			{
				auto &emp{ db.getEmployee(employeeNumber) };
				emp.fire();
				std::cout << std::format("Employee {} terminated.", employeeNumber) << std::endl;
			}
			catch (const std::logic_error &exception)
			{
				std::cerr << std::format("Unable to terminate employee: {}", exception.what()) << std::endl;
			}
		}

		void doPromote(Database &db)
		{
			int employeeNumber{};
			std::cout << "Employee number? ";
			std::cin >> employeeNumber;

			int raiseAmount{};
			std::cout << "How nuch of a raise? ";
			std::cin >> raiseAmount;

			try
			{
				auto &emp{ db.getEmployee(employeeNumber) };
				emp.promote(raiseAmount);
				std::cout << std::format("Employee {} promoted.", employeeNumber) << std::endl;
			}
			catch (const std::logic_error &exception)
			{
				std::cerr << std::format("Unable to promote employee: {}", exception.what()) << std::endl;
			}
		}

		void interface()
		{
			Database db;
			bool done{ false };

			while (done == false)
			{
				int selection{ displayMenu() };
				switch (selection)
				{
					case 0:
					{
						done = true;
						break;
					}
					case 1:
					{
						doHire(db);
						break;
					}
					case 2:
					{
						doFire(db);
						break;
					}
					case 3:
					{
						doPromote(db);
						break;
					}
					case 4:
					{
						db.displayAll();
						break;
					}
					case 5:
					{
						db.displayCurrent();
						break;
					}
					case 6:
					{
						db.displayFormer();
						break;
					}
					default:
					{
						std::cerr << "Unknow command." << std::endl;
						break;
					}
				}
			}
		}
	}
}